//
//  LBSaturationFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 3/29/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBSaturationFilter.h"
#include "LBGraphics.h"

char fShaderStrSaturation[] = ""
"precision mediump float;"

"varying vec2 vtexCoor;"
"varying vec4 vposition;"
"uniform sampler2D texture;"
"uniform float saturation;"


//saturation function (default 1.0)
"vec4 saturationColor(vec4 color, float saturation){"

// Values from "Graphics Shaders: Theory and Practice" by Bailey and Cunningham
"   const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);"

"   lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"   lowp float luminance = dot(textureColor.rgb, luminanceWeighting);"
"   lowp vec3 greyScaleColor = vec3(luminance);"

"        return vec4(mix(greyScaleColor, textureColor.rgb, saturation), textureColor.w);"
        
"}"
//END saturation function


"void main()"
"{"
"    lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"    textureColor = saturationColor(textureColor, saturation);"
"    gl_FragColor = textureColor;"
"}";

LBSaturationFilter::LBSaturationFilter(){
    init(0, fShaderStrSaturation);
    LBGraphics::getInstance()->useProgram(program);
    saturationUniform = program->uniformIndex("saturation");
    setSaturation(1.0f);
}

void LBSaturationFilter::setSaturation(float saturation){
    this->saturation = saturation;
    LBGraphics::getInstance()->useProgram(program);
    glUniform1f(saturationUniform, saturation);
}